package com.example.mvp.view

import android.content.Context
import android.support.v7.widget.AppCompatEditText
import android.text.TextUtils
import android.util.AttributeSet
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection
import cc.vv.lkbasecomponent.base.app.LKBaseApplication
import cc.vv.lkimcomponent.view.edit.IMInputConnection
import cc.vv.lkimcomponent.view.emoji.EmojiParser
import cc.vv.lkimcomponent.view.emoji.ParseEmojiOperate

/**
 * Im文本输入框显示
 * @author  chaohaipeng  on 2018/8/24.
 */
class IMEditText : AppCompatEditText {
    private val ID_PASTE = 16908322
    private val androidTag = "http://schemas.android.com/apk/res/android"
    private var mMaxLength = 0

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        if (null != attrs) {
            this.mMaxLength = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "maxLength", 0)
        }
    }
    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) {
        if (null != attrs) {
            this.mMaxLength = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "maxLength", 0)
        }
    }

    override fun onTextContextMenuItem(id: Int): Boolean {
        if (id == 16908322) {
            var residueLength = this.mMaxLength
            val currentShowTxt = this.getText().toString()
            if (!TextUtils.isEmpty(currentShowTxt)) {
                residueLength = this.mMaxLength - currentShowTxt.length
            }

            val clip = LKBaseApplication.getApplication().getSystemService(Context.CLIPBOARD_SERVICE) as android.content.ClipboardManager
            if (null != clip) {
                var clipText = ""
                if (!clip.hasPrimaryClip()) {

                } else {
                    val clipData = clip.primaryClip
                    val count = clipData.itemCount
                    for (i in 0 until count) {
                        val item = clipData.getItemAt(i)
                        val str = item.coerceToText(LKBaseApplication.getApplication())
                        clipText += str
                    }
                val clipStr = clipText.trim { it <= ' ' }
                var msgContent = EmojiParser.getInstance().parseEmoji(clipStr)
                val currentLength = msgContent.length
                if (!TextUtils.isEmpty(msgContent) && residueLength > 0 && currentLength > residueLength) {
                    msgContent = msgContent.subSequence(0, residueLength).toString()
                    val lastIndexOfEmojiStart = msgContent.lastIndexOf("[e]")
                    if (lastIndexOfEmojiStart > 0) {
                        val lastIndexOfEmojiEnd = msgContent.lastIndexOf("[/e]", lastIndexOfEmojiStart)
                        if (lastIndexOfEmojiEnd < lastIndexOfEmojiStart) {
                            msgContent = msgContent.substring(0, lastIndexOfEmojiStart)
                        }
                    }
                }

                val ssMsgContent = ParseEmojiOperate.getInstance().parseConvertEmoji(msgContent, 20)
                this.append(ssMsgContent)
                }
            }

            return false
        } else {
            return super.onTextContextMenuItem(id)
        }
    }


    override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {
        return IMInputConnection(super.onCreateInputConnection(outAttrs), false)
    }
}